package com.example.zhanglinan.vrdemo.fragment;

import android.content.Intent;
import android.os.SystemClock;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.zhanglinan.vrdemo.R;
import com.example.zhanglinan.vrdemo.activity.VideoDetailActivity;
import com.example.zhanglinan.vrdemo.bean.VideoBean;
import com.facebook.drawee.view.SimpleDraweeView;

import java.text.SimpleDateFormat;
import java.util.List;

/**
 * @author Schutzengel
 *         o(＞﹏＜)o
 *         create on 2017/1/27 14:40
 *         $desc$
 */
public class VideoFragment extends BaseFragment {

    private static final String TAG = "VideoFragment";
    private static final int TYPE_ITEM = 0;  //普通Item View
    private static final int TYPE_FOOTER = 1;  //底部FootView
    //上拉加载更多
    public static final int PULLUP_LOAD_MORE = 0;
    //正在加载中
    public static final int LOADING_MORE = 1;
    //上拉加载更多状态-默认为0
    private int load_more_status = 0;
    private GridLayoutManager mManager;
    //    private LinearLayoutManager mManager;
    private VideoAdapter mAdapter;
    private VideoClickListerner mListerner;

    public VideoFragment() {
    }

    public VideoFragment(List<VideoBean> list) {
        this.list = list;
    }

    private List<VideoBean> list;

    @Override
    public RecyclerView.LayoutManager getManager() {
        mManager = new GridLayoutManager(getActivity(), 3);
//        mManager = new LinearLayoutManager(getActivity());
        mManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                return position == list.size() ? mManager.getSpanCount() : position % 3 + 1;
//                return position % 3 + 1;
            }
        });
        return mManager;
    }

    @Override
    public RecyclerView.Adapter getAdapter() {
        mListerner = new VideoClickListerner();
        mAdapter = new VideoAdapter();
        return mAdapter;
    }

    class VideoAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

        private GridSpanSizeLookop mGridSpanSizeLookup;

//        @Override
//        public void onAttachedToRecyclerView(RecyclerView recyclerView) {
//            super.onAttachedToRecyclerView(recyclerView);
//            if (mGridSpanSizeLookup == null) {
//                mGridSpanSizeLookup = new GridSpanSizeLookop();
//            }
//            mManager.setSpanSizeLookup(mGridSpanSizeLookup);
//        }

        @Override
        public int getItemViewType(int position) {
            if (position + 1 == getItemCount()) {
                return TYPE_FOOTER;
            } else {
                return TYPE_ITEM;
            }
        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view;
            if (viewType == TYPE_ITEM) {
                view = getActivity().getLayoutInflater().inflate(R.layout.fragment_video, parent, false);
                return new VideoHolder(view);
            } else if (viewType == TYPE_FOOTER) {
                view = getActivity().getLayoutInflater().inflate(R.layout.foot_view, parent, false);
                return new FootHolder(view);
            }
            return null;
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            if (holder instanceof VideoHolder) {
                VideoBean videoBean = list.get(position);
                VideoHolder videoHolder = (VideoHolder) holder;
                videoHolder.fbVideo.setImageURI(videoBean.getImg());
                videoHolder.tvDate.setText(new SimpleDateFormat("MM/dd/yyyy").format(videoBean.getDate()));
                videoHolder.tvTitle.setText(videoBean.getTitle());
                videoHolder.llVideo.setTag(videoBean);
                videoHolder.llVideo.setOnClickListener(mListerner);
            } else if (holder instanceof FootHolder) {
                FootHolder footHolder = (FootHolder) holder;
                switch (load_more_status) {
                    case PULLUP_LOAD_MORE:
                        footHolder.tvFt.setText("上拉加载更多");
                        break;
                    case LOADING_MORE:
                        footHolder.tvFt.setText("正在加载更多");
                        break;
                }
            }
        }

        @Override
        public int getItemCount() {
            return list.size() + 1;
        }

        class VideoHolder extends RecyclerView.ViewHolder {

            private final TextView tvTitle;
            private final TextView tvDate;
            private final SimpleDraweeView fbVideo;
            private final LinearLayout llVideo;

            public VideoHolder(View itemView) {
                super(itemView);
                tvTitle = (TextView) itemView.findViewById(R.id.topic_init_title);
                llVideo = (LinearLayout) itemView.findViewById(R.id.video_layout);
                tvDate = (TextView) itemView.findViewById(R.id.date_text);
                fbVideo = (SimpleDraweeView) itemView.findViewById(R.id.topic_init_img);
            }
        }

        class FootHolder extends RecyclerView.ViewHolder {
            private final TextView tvFt;

            public FootHolder(View itemView) {
                super(itemView);
                tvFt = (TextView) itemView.findViewById(R.id.tv_ft);
            }
        }
    }

    public int getCount() {
        return mManager.findLastVisibleItemPosition();
    }

    class VideoClickListerner implements View.OnClickListener {
        @Override
        public void onClick(View view) {
            VideoBean videoBean = (VideoBean) view.getTag();
            Intent intent = new Intent(getActivity(), VideoDetailActivity.class);
            //需要传递的数据:
            intent.putExtra("title", videoBean.getTitle());
            intent.putExtra("img", videoBean.getImg());
            intent.putExtra("text", videoBean.getText());
            intent.putExtra("play", videoBean.getPlay());
            getActivity().startActivity(intent);
        }
    }

    @Override
    public void onFresh() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                SystemClock.sleep(2000);
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        onComplete();
                        Toast.makeText(getActivity(), "刷新完成", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        }).start();
    }

    @Override
    public void onAdd(RecyclerView recyclerView, int newState) {
        load_more_status = LOADING_MORE;
        if (newState == RecyclerView.SCROLL_STATE_IDLE && getCount() + 1 == mAdapter.getItemCount()) {
//            Toast.makeText(getActivity(), "上拉加载中", Toast.LENGTH_SHORT).show();
            new Thread(new Runnable() {
                @Override
                public void run() {
                    load_more_status = PULLUP_LOAD_MORE;
                    SystemClock.sleep(2000);
                    list.addAll(list);
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            mAdapter.notifyDataSetChanged();
                            Toast.makeText(getActivity(), "加载完成", Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            }).start();
        }
    }

    class GridSpanSizeLookop extends GridLayoutManager.SpanSizeLookup {
        @Override
        public int getSpanSize(int position) {
//            Log.d(TAG, position + "xx" + list.size() + 1);
            if (position == list.size()) {
                return mManager.getSpanCount();
            }
            return 1;
        }
    }
}
